﻿// Copyright 2009 Mike Geise
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Falcon.Core.Domain
{
	using System;
	using Falcon.Core.Authentication;
	using Falcon.Core.Validation;

	public class Attachment : EntityBase<Attachment>
	{
		private User _user;

		/// <summary>
		/// Initializes a new instance of the <see cref="Attachment"/> class.
		/// </summary>
		public Attachment()
		{
			this.Identity = User.Identity;
			base.DeterminePermission(Roles.Attachments);
		}

		/// <summary>
		/// Gets or sets the user id.
		/// </summary>
		/// <value>The user id.</value>
		public virtual int UserId
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the name of the file.
		/// </summary>
		/// <value>The name of the file.</value>
		public virtual string FileName
		{
			get; 
			set;
		}

		/// <summary>
		/// Gets or sets the name of the file on disk.
		/// </summary>
		/// <value>The name of the file disk.</value>
		public virtual string FileDiskName
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the file path.
		/// </summary>
		/// <value>The file path.</value>
		public virtual string FilePath
		{
			get;
			set;
		}

		/// <summary>
		/// Gets the full path.
		/// </summary>
		/// <value>The full path.</value>
		public virtual string FullPath
		{
			get
			{
				return string.Concat(this.FilePath, "/", this.FileDiskName);
			}
		}

		/// <summary>
		/// Gets or sets the type of the file.
		/// </summary>
		/// <value>The type of the file.</value>
		public virtual string FileType
		{
			get; 
			set;
		}

		/// <summary>
		/// Gets or sets the file extension.
		/// </summary>
		/// <value>The file extension.</value>
		public virtual string FileExtension
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the size of the file.
		/// </summary>
		/// <value>The size of the file.</value>
		public virtual int FileSize
		{
			get; 
			set;
		}

		/// <summary>
		/// Gets or sets the width.
		/// </summary>
		/// <value>The width.</value>
		public virtual int Width
		{
			get; 
			set;
		}

		/// <summary>
		/// Gets or sets the height.
		/// </summary>
		/// <value>The height.</value>
		public virtual int Height
		{
			get; 
			set;
		}

		/// <summary>
		/// Gets a value indicating whether this instance is owner.
		/// </summary>
		/// <value><c>true</c> if this instance is owner; otherwise, <c>false</c>.</value>
		public virtual bool IsOwner
		{
			get
			{
				return this.Identity != null && this.UserId.Equals(this.Identity.Id);
			}
		}

		/// <summary>
		/// Gets or sets a value indicating whether this instance can view.
		/// </summary>
		/// <value><c>true</c> if this instance can view; otherwise, <c>false</c>.</value>
		public override bool CanView
		{
			get
			{
				return base.CanView && this.IsOwner;
			}
		}

		/// <summary>
		/// Gets or sets a value indicating whether this instance can update.
		/// </summary>
		/// <value>
		/// 	<c>true</c> if this instance can update; otherwise, <c>false</c>.
		/// </value>
		public override bool CanUpdate
		{
			get
			{
				return base.CanUpdate && this.IsOwner;
			}
		}

		/// <summary>
		/// Gets or sets a value indicating whether this instance can delete.
		/// </summary>
		/// <value>
		/// 	<c>true</c> if this instance can delete; otherwise, <c>false</c>.
		/// </value>
		public override bool CanDelete
		{
			get
			{
				return base.CanDelete && this.IsOwner;
			}
		}

		/// <summary>
		/// Gets or sets the date created.
		/// </summary>
		/// <value>The created.</value>
		public virtual DateTime Created
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the date updated.
		/// </summary>
		/// <value>The updated.</value>
		public virtual DateTime Updated
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the user.
		/// </summary>
		/// <value>The user.</value>
		public virtual User User
		{
			get
			{
				return _user;
			}
			set
			{
				_user = value;

				if (_user != null)
				{
					this.UserId = _user.Id;
				}
			}
		}

		/// <summary>
		/// Gets or sets the identity.
		/// </summary>
		/// <value>The identity.</value>
		public virtual WebUserIdentity Identity
		{
			get;
			set;
		}

		/// <summary>
		/// Validation rules for this instance.
		/// </summary>
		/// <returns></returns>
		public override Validator<Attachment> Rules()
		{
			return base.Rules();
		}

		/// <summary>
		/// Validates this instance.
		/// </summary>
		public override void Validate()
		{
			this.Rules().Validate(this);
		}
	}
}